/*
 PMProfile.m
 Date Created: February 17, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "PMProfile.h"


@implementation PMProfile 

@dynamic active;
@dynamic sortingGameLaunched;
@dynamic recyclingToolsLaunched;
@dynamic profileName;
@dynamic scores;
@dynamic recyclingLog;

- (BOOL)active {
  [self willAccessValueForKey:@"active"];
  NSNumber *activeObj=[self primitiveValueForKey:@"active"];
  [self didAccessValueForKey:@"active"];
  return activeObj.boolValue;
}

- (void)setActive:(BOOL)newActive {
  [self willChangeValueForKey:@"active"];
  [self setPrimitiveValue:[NSNumber numberWithBool:newActive] forKey:@"active"];
  [self didChangeValueForKey:@"active"];
}

- (BOOL)sortingGameLaunched {
  [self willAccessValueForKey:@"sortingGameLaunched"];
  NSNumber *sortingGameLaunchedObj=[self primitiveValueForKey:@"sortingGameLaunched"];
  [self didAccessValueForKey:@"sortingGameLaunched"];
  return sortingGameLaunchedObj.boolValue;
}

- (void)setSortingGameLaunched:(BOOL)newSortingGameLaunched {
  [self willChangeValueForKey:@"sortingGameLaunched"];
  [self setPrimitiveValue:[NSNumber numberWithBool:newSortingGameLaunched] forKey:@"sortingGameLaunched"];
  [self didChangeValueForKey:@"sortingGameLaunched"];
}

- (BOOL)recyclingToolsLaunched {
  [self willAccessValueForKey:@"recyclingToolsLaunched"];
  NSNumber *recyclingToolsLaunchedObj=[self primitiveValueForKey:@"recyclingToolsLaunched"];
  [self didAccessValueForKey:@"recyclingToolsLaunched"];
  return recyclingToolsLaunchedObj.boolValue;
}

- (void)setRecyclingToolsLaunched:(BOOL)newRecyclingToolsLaunched {
  [self willChangeValueForKey:@"recyclingToolsLaunched"];
  [self setPrimitiveValue:[NSNumber numberWithBool:newRecyclingToolsLaunched] forKey:@"recyclingToolsLaunched"];
  [self didChangeValueForKey:@"recyclingToolsLaunched"];
}

- (void)recordScore:(int)score achievedOn:(NSDate *)date {
  NSManagedObject *scoreObj=[NSEntityDescription insertNewObjectForEntityForName:@"Score" 
                                                          inManagedObjectContext:[self managedObjectContext]];
  
  [scoreObj setValue:[NSNumber numberWithInt:score] forKey:@"score"];
  [scoreObj setValue:date forKey:@"dateTime"];
  
  [self addScoresObject:scoreObj];
  NSLog(@"Scores object added.. %d",score);
  NSError *error;
  if(![[self managedObjectContext] save:&error]) {
    // TODO handle error
    NSLog(@"Error saving score..");
  }
}

// Initialize some relationships
- (void)awakeFromInsert {
  RLRecyclingLog *log=(RLRecyclingLog *)[NSEntityDescription insertNewObjectForEntityForName:@"RecyclingLog" 
                                                                        inManagedObjectContext:[self managedObjectContext]];
  self.recyclingLog=log;
}

@end
